package com.eascs.workflow.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.workflow.sys.dao.HREmplAuditorDao;
import com.eascs.workflow.sys.entity.HREmployeeAuditorEntity;
import com.eascs.workflow.sys.vo.HREmployeeAuditor;

@Service
public class HREmplAuditorService {
	
	@Autowired
	HREmplAuditorDao hrEmplAuditorDao;

	/**
	 * 分页查询-人员关系表
	 * @param entity
	 * @return
	 */
	public PageResult<Object[]> findO2oByPage(HREmployeeAuditor entity) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getO2oIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT t.EMPLOYEEAUDITORNO emplAudiNo,t.EMPLOYEENAME employeeName,m.name auditorType,t.AUDITORNAME auditorName,t.op_psn,t.op_date ");
		querySql.append(" FROM hr_employeeauditor t ");
		querySql.append(" LEFT JOIN wf_dictionary_data m on m.code = t.AUDITORTYPE AND m.is_lock = 'N' ");
		querySql.append(" LEFT JOIN wf_dictionary_type y on m.type = y.id ");
		querySql.append(" WHERE 1=1 ");
		querySql.append(" AND `is_del`='N'  ");//删除标记为N的表示没删除

		if(!StringUtil.isEmpty(entity.getEmployeeName())){
			querySql.append(" AND t.EMPLOYEENAME LIKE ? ");
			sqlPageQuery.addLikeParam(entity.getEmployeeName());
		}
		if(!StringUtil.isEmpty(entity.getAuditorType())){
			querySql.append(" AND t.AUDITORTYPE = ? ");
			sqlPageQuery.addParam(entity.getAuditorType());
		}
		if(!StringUtil.isEmpty(entity.getAuditorName())){
			querySql.append(" AND t.AUDITORNAME LIKE ? ");
			sqlPageQuery.addLikeParam(entity.getAuditorName());
		}
		//根据人员拼音升序排序,根据操作时间降序排序
		querySql.append(" ORDER BY CONVERT(t.EMPLOYEENAME USING gbk) ASC, t.op_date DESC ");
		//querySql.append(" GROUP BY OL.LOANAPPLY_NO ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return hrEmplAuditorDao.findDataWithPage(sqlPageQuery);
	}

	/**
	 * 伪删除-删除人员关系信息
	 * @param emplAudiNo
	 * @return
	 */
	public boolean deleteHrEmplAuditor(HREmployeeAuditorEntity hrEmployeeAuditorEntity) {
		int flag = hrEmplAuditorDao.updateById(hrEmployeeAuditorEntity);//更新
		//int flag = hrEmplAuditorDao.execute("update hr_employeeauditor t set t.is_del ='Y' where t.EMPLOYEEAUDITORNO = ? ", emplAudiNo);
		return flag > 0 ? true : false;
	}
	
	/**
	 * 伪删除-删除人员关系信息(Not use)
	 * @param emplAudiNo
	 * @return
	 */
	public boolean deleteHrEmplAuditor(HREmployeeAuditor hrEmployeeAuditor) {
//		int flag = hrEmplAuditorDao.execute(
//				"update hr_employeeauditor t set t.is_del ='Y',t.op_psn=?,t.op_date=? where t.EMPLOYEEAUDITORNO = ? ",
//				hrEmployeeAuditor.getOp_psn(),
//				hrEmployeeAuditor.getOp_date(),
//				hrEmployeeAuditor.getEmployeeAuditorNo());
		int flag = hrEmplAuditorDao.execute(
				"update hr_employeeauditor t set t.is_del ='Y' where t.EMPLOYEEAUDITORNO = ? ",
				hrEmployeeAuditor.getEmployeeAuditorNo());
		return flag > 0 ? true : false;
	}

	
	/**
	 * 根据人员关系id查询信息【表单】
	 * @param employeeAuditorNo
	 * @return
	 */
	public HREmployeeAuditor getHrEmplAuditorByNo(String employeeAuditorNo) {
		String sql = "SELECT t.* FROM hr_employeeauditor t WHERE 1=1 AND t.is_del='N' AND t.EMPLOYEEAUDITORNO = ?";
		return hrEmplAuditorDao.findEntity(sql, HREmployeeAuditor.class, employeeAuditorNo);
	}
	
	public HREmployeeAuditorEntity getHrEmplAuditor(String employeeAuditorNo) {
		String sql = "SELECT t.* FROM hr_employeeauditor t WHERE 1=1 AND t.is_del='N' AND t.EMPLOYEEAUDITORNO = ?";
		return hrEmplAuditorDao.findEntity(sql, HREmployeeAuditorEntity.class, employeeAuditorNo);
	}
	
	/**
	 * 根据人员关系id查询信息
	 * @param employeeAuditorNo
	 * @return
	 */
	public HREmployeeAuditorEntity getHREmployeeAuditorEntityByNo(String employeeAuditorNo) {
		String sql = "SELECT t.* FROM hr_employeeauditor t WHERE 1=1 AND t.is_del='N' AND t.EMPLOYEEAUDITORNO = ?";
		return hrEmplAuditorDao.findEntity(sql, HREmployeeAuditorEntity.class, employeeAuditorNo);
	}

	/**
	 * 保存更新数据库
	 * @param hrEmployeeAuditorEntity
	 * @return
	 */
	public int update(HREmployeeAuditorEntity hrEmployeeAuditorEntity) {
		return hrEmplAuditorDao.updateById(hrEmployeeAuditorEntity);
	}

	/**
	 * 根据人员No查该人员的集合list
	 * @param hrEmployeeAuditorEntity
	 * @return
	 */
	public List<HREmployeeAuditorEntity> findRelationEntityByEmployeeNo(HREmployeeAuditorEntity hrEmployeeAuditorEntity) {
		return hrEmplAuditorDao.findRelationEntityByEmployeeNo(hrEmployeeAuditorEntity);
	}

	/**
	 * 根据人员No查该人员的集合list
	 * @param entity
	 * @return
	 */
	public List<HREmployeeAuditorEntity> findRelationByEmployeeNo(HREmployeeAuditorEntity entity) {
		return hrEmplAuditorDao.findRelationByEmployeeNo(entity);
	}

	/**
	 * 根据人员No查询关系和关系人是否存在
	 * @param entity
	 * @return
	 */
	public boolean findRelationAndAuditorByEmployeeNo(HREmployeeAuditor entity) {
		String sql = "SELECT count(*) FROM hr_employeeauditor t WHERE 1=1 AND t.is_del='N' AND t.EMPLOYEENO = ? AND t.AUDITORTYPE = ? AND t.AUDITORNAME = ?";
		int flag = hrEmplAuditorDao.findInteger(sql, entity.getEmployeeNo(),entity.getAuditorType(),entity.getAuditorName());
		return flag > 0 ? true : false;
	}
	
	
	/**
	 * 根据EmployeeNo和auditorType查询个数
	 * @param employNo
	 * @param auditorType
	 * @return
	 */
	public int getCountByEmplNoAndAudType(String employNo, String auditorType) {
		String sql = "SELECT COUNT(*) from hr_employeeauditor t where 1=1 AND t.is_del='N' AND t.EMPLOYEENO = ? and t.AUDITORTYPE = ?;";
		return hrEmplAuditorDao.findInteger(sql, employNo, auditorType );

	}

	/**
	 * 添加-插入数据库
	 * @param hrEmployeeAuditorEntity
	 * @return
	 */
	public HREmployeeAuditorEntity saveEmployeeAuditor(HREmployeeAuditorEntity hrEmployeeAuditorEntity) {
		return hrEmplAuditorDao.insert(hrEmployeeAuditorEntity);
	}
	
	public HREmployeeAuditorEntity getEmployeeEntity(String employNo, String auditorType ,String auditorNo) {
		return hrEmplAuditorDao.getEmployeeEntity(employNo, auditorType, auditorNo);
	}
	
	public List<HREmployeeAuditorEntity> getEmployeeEntity(String employNo, String auditorType) {
		return hrEmplAuditorDao.getEmployeeEntity(employNo, auditorType);
	}


	public List<HREmployeeAuditorEntity> getAuditorWithNo(String employNo) {
		return hrEmplAuditorDao.getAuditorWithNo(employNo);
	}
	

}
